package demo;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserMapper {
    public int insert(User user) {
        int num = 0;
        try(Connection connection=DBUtil.getConnection()) {
            String sql="INSERT INTO user(username,password,nickname,age)\n" +
                    "        VALUES(?,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            num=ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    /**
     *
     * @param username
     * @return
     */
    public User selectByUsername(String username) {
        try (Connection connection=DBUtil.getConnection()){
            String sql="select id,username,nickname,age,password from user where username = ?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs=ps.executeQuery();
            if(rs.next()){
                User user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                user.setPassword(rs.getString("password"));
                return user;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
//    public User selectByUsernameAndPassword(String username,String password) {
//        try (Connection connection=DBUtil.getConnection()){
//            String sql="select id,username,nickname,age,password from user where username = ?" +
//                    "and password=?";
//            PreparedStatement ps=connection.prepareStatement(sql);
//            ps.setString(1,username);
//            ps.setString(2,password);
//            ResultSet rs=ps.executeQuery();
//            if(rs.next()){
//                User user=new User();
//                user.setId(rs.getInt("id"));
//                user.setUsername(rs.getString("username"));
//                user.setNickname(rs.getString("nickname"));
//                user.setAge(rs.getInt("age"));
//                user.setPassword(rs.getString("password"));
//                return user;
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//        return null;
//    }
    public int update(User user){
        try(Connection connection=DBUtil.getConnection()) {
//            UserMapper um=new UserMapper();
//            User userFind=um.selectByUsername(user.getUsername());
//            if(userFind==null){
//                return 0;
//            }else if( userFind.getPassword().equals(user.getPassword())){
//                System.out.println("密码错误");
//            }

            String sql="update user\n" +
                    "        set password=?,nickname=?,age=?\n" +
                    "        where username =?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,user.getPassword());
            ps.setString(2,user.getNickname());
            ps.setInt(3,user.getAge());
            ps.setString(4,user.getUsername());

            if(ps.executeUpdate()>0){
                return ps.executeUpdate();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    public List<User> selectAll(){
        List<User> list = new ArrayList<>();
        try(Connection connection=DBUtil.getConnection()) {
            String sql="select id,username,nickname,age,password from user";
            Statement ps=connection.createStatement();
            ResultSet rs=ps.executeQuery(sql);
            System.out.println("查询成功");
            while(rs.next()){
                User user=new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                user.setPassword(rs.getString("password"));
                list.add(user);
            }

        }catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
        return list;
    }
}
